package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import cc.edt.iceutils3.date.IceDateOperationUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.dao.OrderChannelDao;
import com.edt.ichannel.api.entiy.local.*;
import com.edt.ichannel.api.service.ExpansionService;
import com.edt.ichannel.api.service.OrderChannelService;
import com.edt.ichannel.api.service.StaffService;
import com.edt.ichannel.api.service.WorpOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 肖瑶
 */
@Service
@Slf4j
public class OrderChannelServiceImpl implements OrderChannelService {

    private static final int SYN_SYSTEM_TYPE_JF = 1;
    private static final int SYN_SYSTEM_TYPE_XXH = 2;
    @Resource
    private OrderChannelDao orderChannelDao;

    @Resource
    private WorpOrderService worpOrderService;
    @Resource
    private ExpansionService expansionService;
    @Resource
    private StaffService staffService;

    /**
     * 受理结果回调
     *
     * @param ordercode  订单流水
     * @param orderstate 订单状态
     * @param remark     退单标注
     * @return 返回值
     */
    @Override
    public ActionResult orderBack(String ordercode, String orderstate, String remark) {
        ActionResult actionResult = new ActionResult();
        OrderChannel orderChannel = new OrderChannel();
        orderChannel.setOrderCode(ordercode);
        try {
            if (Integer.parseInt(orderstate) == 2) {
                //订单在途
                orderChannel.setOrderState(6);
                orderChannel.setStateDate(new Date());
                if (!IceStringUtils.isBlank(remark)) {
                    orderChannel.setStateRemark(remark);
                }
            } else if (Integer.parseInt(orderstate) == 3) {
                //订单完结
                orderChannel.setOrderState(7);
                orderChannel.setStateDate(new Date());
                if (!IceStringUtils.isBlank(remark)) {
                    orderChannel.setStateRemark(remark);
                }
            } else if (Integer.parseInt(orderstate) == 4) {
                //订单退单
                orderChannel.setOrderState(4);
                orderChannel.setQuitDate(new Date());
                if (!IceStringUtils.isBlank(remark)) {
                    orderChannel.setQuitRemark(remark);
                }
            } else {
                //解析错误
                actionResult.setSuccess(false);
                actionResult.setMessage("订单状态不在受理结果范围内");
                return actionResult;
            }
            orderChannelDao.updateOrderChannelState(orderChannel);
            actionResult.setSuccess(true);
            actionResult.setMessage("受理成功");
        } catch (NumberFormatException ne) {
            ne.printStackTrace();
            actionResult.setSuccess(false);
            actionResult.setMessage("订单状态解析错误");
        } catch (Exception e) {
            e.printStackTrace();
            actionResult.setMessage(e.getMessage());
            actionResult.setSuccess(false);
        }
        return actionResult;
    }

    @Override
    public List<OrderChannel> selectOrderChannelListJF(String dateStr) {
        return orderChannelDao.selectOrderChannelList(dateStr, SYN_SYSTEM_TYPE_JF);
    }

    @Override
    public List<OrderChannel> selectOrderChannelMonthListJF(String dateStr) {
        return orderChannelDao.selectOrderChannelMonthList(dateStr, SYN_SYSTEM_TYPE_JF);
    }

    @Override
    public List<OrderChannel> selectOrderChannelListXXH(String dateStr) {
        return orderChannelDao.selectOrderChannelListXXH(dateStr, SYN_SYSTEM_TYPE_XXH);
    }

    @Override
    public List<OrderChannel> selectOrderChannelMonthListXXH(String dateStr) {
        return orderChannelDao.selectOrderChannelMonthList(dateStr, SYN_SYSTEM_TYPE_XXH);
    }

    @Override
    public List<OrderChannel> selectActivityStaff(String dataStr) {
        return orderChannelDao.selectActivityStaff(dataStr);
    }

    @Override
    public List<OrderChannel> selectOrderChannelCkWithoutCommissionByDay(String dayStr) {
        return orderChannelDao.selectOrderChannelCkWithoutCommissionByDay(dayStr);
    }

    @Override
    public List<OrderChannel> selectOrderChannelDay(String dayStr) {
        return orderChannelDao.selectOrderChannelDay(dayStr, SYN_SYSTEM_TYPE_JF);
    }

    @Override
    public List<OrderChannel> selectOrderChannelKdWithoutCommissionByDay(String dayStr) {
        return orderChannelDao.selectOrderChannelKdWithoutCommissionByDay(dayStr);
    }

    @Override
    public OrderChannel selectOrderChannelByNumberPhone(String phoneNumber) {
        return orderChannelDao.selectOrderChannelByNumberPhone(phoneNumber);
    }


    @Override
    public void setExpansionTypeAndLevl(OrderCreditKd orderCreditKd) {
        if(orderCreditKd.getChannelId() == null || orderCreditKd.getChannelLevel() == null || orderCreditKd.getChannelType() == null){
            //判断订单是人还是渠道的

            int expansionType = 1;
            String orderType = worpOrderService.getOrderType(orderCreditKd.getOrderCode());
            //是人，则是0级订单
            if (CommonConstant.ORDER_TYPE_STAFF.equals(orderType)) {

                // 0 级订单，直接给佣金
                String staffId = worpOrderService.getChannelIdByOrderCode(orderCreditKd.getOrderCode());
                Staff staff = staffService.getStaffById(staffId);
                if (staff != null) {
                    expansionType = staff.getType();
                    orderCreditKd.setChannelId(staff.getId());
                    orderCreditKd.setChannelLevel(CommonConstant.EXPANSION_LEVEL_ZERO);
                } else {
                    log.info("ordercode:" + orderCreditKd.getOrderCode() + "0级订单没有查询到0级");
                }
            } else {
                String expansionId = worpOrderService.getChannelIdByOrderCode(orderCreditKd.getOrderCode());
                Expansion expansion = expansionService.getExpansionById(expansionId);
                if (expansion != null) {
                    orderCreditKd.setChannelLevel(expansion.getExpansionLevel());
                    expansionType = expansion.getType();
                    orderCreditKd.setChannelId(expansion.getId());
                } else {
                    log.info("ordercode:" + orderCreditKd.getOrderCode() + "订单没有查询到合伙人");
                }

            }
            orderCreditKd.setChannelType(expansionType);
        }

    }

    @Override
    public void setExpansionTypeAndLevl(OrderCreditCK orderCreditCK) {
        if(orderCreditCK.getChannelId() == null || orderCreditCK.getChannelLevel() == null || orderCreditCK.getChannelType() == null){
            //判断订单是人还是渠道的

            int expansionType = 1;
            String orderType = worpOrderService.getOrderType(orderCreditCK.getOrderCode());
            //是人，则是0级订单
            if (CommonConstant.ORDER_TYPE_STAFF.equals(orderType)) {

                // 0 级订单，直接给佣金
                String staffId = worpOrderService.getChannelIdByOrderCode(orderCreditCK.getOrderCode());
                Staff staff = staffService.getStaffById(staffId);
                if (staff != null) {
                    expansionType = staff.getType();
                    orderCreditCK.setChannelId(staff.getId());
                    orderCreditCK.setChannelLevel(CommonConstant.EXPANSION_LEVEL_ZERO);
                } else {
                    log.info("ordercode:" + orderCreditCK.getOrderCode() + "0级订单没有查询到0级");
                }
            } else {
                String expansionId = worpOrderService.getChannelIdByOrderCode(orderCreditCK.getOrderCode());
                Expansion expansion = expansionService.getExpansionById(expansionId);
                if (expansion != null) {
                    orderCreditCK.setChannelLevel(expansion.getExpansionLevel());
                    expansionType = expansion.getType();
                    orderCreditCK.setChannelId(expansion.getId());
                } else {
                    log.info("ordercode:" + orderCreditCK.getOrderCode() + "订单没有查询到合伙人");
                }

            }
            orderCreditCK.setChannelType(expansionType);
        }

    }

    @Override
    public void setExpansionTypeAndLevl(OrderCreditBf orderCreditBf) {
        if(orderCreditBf.getChannelId() == null || orderCreditBf.getChannelLevel() == null || orderCreditBf.getChannelType() == null){
            //判断订单是人还是渠道的

            int expansionType = 1;
            String orderType = worpOrderService.getOrderType(orderCreditBf.getOrderCode());
            //是人，则是0级订单
            if (CommonConstant.ORDER_TYPE_STAFF.equals(orderType)) {

                // 0 级订单，直接给佣金
                String staffId = worpOrderService.getChannelIdByOrderCode(orderCreditBf.getOrderCode());
                Staff staff = staffService.getStaffById(staffId);
                if (staff != null) {
                    expansionType = staff.getType();
                    orderCreditBf.setChannelId(staff.getId());
                    orderCreditBf.setChannelLevel(CommonConstant.EXPANSION_LEVEL_ZERO);
                } else {
                    log.info("ordercode:" + orderCreditBf.getOrderCode() + "0级订单没有查询到0级");
                }
            } else {
                String expansionId = worpOrderService.getChannelIdByOrderCode(orderCreditBf.getOrderCode());
                Expansion expansion = expansionService.getExpansionById(expansionId);
                if (expansion != null) {
                    orderCreditBf.setChannelLevel(expansion.getExpansionLevel());
                    expansionType = expansion.getType();
                    orderCreditBf.setChannelId(expansion.getId());
                } else {
                    log.info("ordercode:" + orderCreditBf.getOrderCode() + "订单没有查询到合伙人");
                }

            }
            orderCreditBf.setChannelType(expansionType);
        }

    }

    @Override
    public void setExpansionTypeAndLevl(OrderCreditMonth orderCreditMonth) {
        if(orderCreditMonth.getChannelId() == null || orderCreditMonth.getChannelLevel() == null || orderCreditMonth.getChannelType() == null){
            //判断订单是人还是渠道的

            int expansionType = 1;
            String orderType = worpOrderService.getOrderType(orderCreditMonth.getOrderCode());
            //是人，则是0级订单
            if (CommonConstant.ORDER_TYPE_STAFF.equals(orderType)) {

                // 0 级订单，直接给佣金
                String staffId = worpOrderService.getChannelIdByOrderCode(orderCreditMonth.getOrderCode());
                Staff staff = staffService.getStaffById(staffId);
                if (staff != null) {
                    expansionType = staff.getType();
                    orderCreditMonth.setChannelId(staff.getId());
                    orderCreditMonth.setChannelLevel(CommonConstant.EXPANSION_LEVEL_ZERO);
                } else {
                    log.info("ordercode:" + orderCreditMonth.getOrderCode() + "0级订单没有查询到0级");
                }
            } else {
                String expansionId = worpOrderService.getChannelIdByOrderCode(orderCreditMonth.getOrderCode());
                Expansion expansion = expansionService.getExpansionById(expansionId);
                if (expansion != null) {
                    orderCreditMonth.setChannelLevel(expansion.getExpansionLevel());
                    expansionType = expansion.getType();
                    orderCreditMonth.setChannelId(expansion.getId());
                } else {
                    log.info("ordercode:" + orderCreditMonth.getOrderCode() + "订单没有查询到合伙人");
                }

            }
            orderCreditMonth.setChannelType(expansionType);
        }

    }

    @Override
    public OrderChannel selectOrderCHannelByOrderCode(String orderCode) {
        return orderChannelDao.selectOrderChannelByOrderCode(orderCode);
    }

    @Override
    public List<OrderChannel> selectOrderChannelCkCWithoutCommissionByDay(String day) {
        return orderChannelDao.selectOrderChannelCkCWithoutCommissionByDay(day);
    }

    @Override
    public List<OrderChannel> selectOrderChannelKdCWithoutCommissionByDay(String day) {
        return orderChannelDao.selectOrderChannelKdCWithoutCommissionByDay(day);
    }

    @Override
    public List<OrderChannel> getNeedBFOrder(String cityCode, int continuitionDay, String productId, String typeStr) {
        Date ruleDate = IceDateOperationUtils.addDays(new Date(),-continuitionDay);
        return orderChannelDao.getNeedBFOrder(cityCode,productId, typeStr,ruleDate);
    }

    @Override
    public void updateD05AddToIsPushOver() {
        orderChannelDao.updateD05AddToIsPushOver();
    }

    @Override
    public List<OrderChannel> selectOrderChannelDayForFXQYZX(String dayStr) {
        return orderChannelDao.selectOrderChannelDayForFXQYZX(dayStr);
    }


}
